package com.fr.report.cellElement;

import com.fr.base.ColumnRow;
import com.fr.base.ColumnRowRelated;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.antlr.ANTLRException;
import com.fr.base.core.serializable.ImageSerializable;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.script.Calculator;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Formula
  implements XMLable, ColumnRowRelated
{
  private String content = "";
  private transient Object result = null;
  private boolean reserveInResult = false;
  private boolean reserveOnWrite = true;

  public Formula()
  {
  }

  public Formula(String paramString)
  {
    setContent(paramString);
  }

  public String getContent()
  {
    if (this.content.trim().startsWith("="))
      return this.content;
    return "=" + this.content;
  }

  public void setContent(String paramString)
  {
    this.content = paramString;
    if (this.content.trim().startsWith("="))
      this.content = this.content.trim().substring(1);
  }

  public Object getResult()
  {
    return this.result;
  }

  public void setResult(Object paramObject)
  {
    this.result = paramObject;
  }

  public boolean isReserveInResult()
  {
    return this.reserveInResult;
  }

  public void setReserveInResult(boolean paramBoolean)
  {
    this.reserveInResult = paramBoolean;
  }

  public boolean isReserveOnWrite()
  {
    return this.reserveOnWrite;
  }

  public void setReserveOnWrite(boolean paramBoolean)
  {
    this.reserveOnWrite = paramBoolean;
  }

  public boolean isEmpty()
  {
    return "=".equals(getContent());
  }

  public ColumnRow[] getRelatedColumnRows()
  {
    if (StringUtils.isNotBlank(this.content))
      try
      {
        return Calculator.relatedColumnRowArray(this.content);
      }
      catch (ANTLRException localANTLRException)
      {
        FRContext.getLogger().log(Level.WARNING, localANTLRException.getMessage(), localANTLRException);
      }
    return new ColumnRow[0];
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws ClassNotFoundException, IOException
  {
    paramObjectInputStream.defaultReadObject();
    Object localObject = paramObjectInputStream.readObject();
    if (localObject != null)
      if (localObject instanceof ImageSerializable)
        this.result = ((ImageSerializable)localObject).getImage();
      else
        this.result = localObject;
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
    if ((this.result != null) && (this.result instanceof Image))
    {
      ImageSerializable localImageSerializable = new ImageSerializable((Image)this.result);
      paramObjectOutputStream.writeObject(localImageSerializable);
    }
    else
    {
      paramObjectOutputStream.writeObject(this.result);
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if ("Attributes".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("reserveExecute")) != null)
          setReserveInResult(Boolean.valueOf(str1).booleanValue());
        if ((str1 = paramXMLableReader.getAttr("reserveInWeb")) != null)
          setReserveOnWrite(Boolean.valueOf(str1).booleanValue());
        if ((str1 = paramXMLableReader.getElementValue()) != null)
          setContent(str1);
      }
      else if ("O".equals(str2))
      {
        setResult(ReportXMLUtils.readObject(paramXMLableReader));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Attributes");
    if (isReserveInResult())
      paramXMLPrintWriter.attr("reserveExecute", isReserveInResult());
    if (!(isReserveOnWrite()))
      paramXMLPrintWriter.attr("reserveInWeb", isReserveOnWrite());
    paramXMLPrintWriter.textNode(getContent());
    paramXMLPrintWriter.end();
    Object localObject = this.result;
    if (localObject != null)
      ReportXMLUtils.writeObject(paramXMLPrintWriter, localObject);
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof Formula) && (isReserveInResult() == ((Formula)paramObject).isReserveInResult()) && (isReserveOnWrite() == ((Formula)paramObject).isReserveOnWrite()) && (ComparatorUtils.equals(getContent(), ((Formula)paramObject).getContent())));
  }

  public String toString()
  {
    return getContent();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}